package logic

import (
	"context"
	"encoding/json"
	"manuscript-service/models/cache"
	"manuscript-service/models/mysql"
	"time"

	"manuscript-service/goodsrpc/goodsrpc"
	"manuscript-service/goodsrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *goodsrpc.GoodsListRequest) (*goodsrpc.GoodsListResponse, error) {
	// todo: add your logic here and delete this line
	var goodsList []*goodsrpc.GoodsList
	result, _ := cache.Rdb.Get(l.ctx, "goodsList").Result()
	if result == "" {
		goodsList = mysql.GoodsList(mysql.DB, goodsList)
		marshal, _ := json.Marshal(result)
		cache.Rdb.Set(l.ctx, "goodsList", marshal, time.Minute*120)
	} else {
		json.Unmarshal([]byte(result), &goodsList)
	}

	return &goodsrpc.GoodsListResponse{
		List: goodsList,
	}, nil
}
